package week4;

import com.sun.org.apache.bcel.internal.generic.NEW;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

/**
 * @author The Setting sun
 * @create 2021-09-30 14:38
 */
class MyPaint extends JPanel {
    BufferedImage image;

    public MyPaint(BufferedImage image) {
        this.image = image;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(image, 0, 0, null);
    }
}

public class Draw {
    int startX;
    int startY;
    int endX;
    int endY;
    String str;
    String color = new String();

    public void start() {
        JFrame frame = new JFrame();

        BufferedImage image = new BufferedImage(700, 560, BufferedImage.TYPE_3BYTE_BGR);


        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        panel.setSize(700, 40);

        JButton button1 = new JButton("开始");
        JButton button2 = new JButton("清除");
        JButton button3 = new JButton("直线");
        JButton button4 = new JButton("矩形");
        JButton button5 = new JButton("圆形");

        Graphics g = image.getGraphics();
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                str = "开始";
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                str = "清除";
            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                str = "直线";
            }
        });

        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                str = "矩形";
            }
        });

        button5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                str = "圆形";
            }
        });

        JRadioButton jb1 = new JRadioButton();
        jb1.setBackground(Color.BLUE);
        JRadioButton jb2 = new JRadioButton();
        jb2.setBackground(Color.PINK);
        JRadioButton jb3 = new JRadioButton();
        jb3.setBackground(Color.GREEN);

        ButtonGroup bg = new ButtonGroup();
        bg.add(jb1);
        bg.add(jb2);
        bg.add(jb3);

        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                color = "蓝色";
            }
        });

        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                color = "粉色";
            }
        });

        jb3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                color = "绿色";
            }
        });


        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button4);
        panel.add(button5);
        panel.add(jb1);
        panel.add(jb2);
        panel.add(jb3);

        MyPaint mp = new MyPaint(image);
        mp.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                startX = e.getX();
                startY = e.getY();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                endX = e.getX();
                endY = e.getY();

                Graphics g = image.getGraphics();
                g.setColor(Color.WHITE);

                switch (str){
                    case "开始":
                        g.drawLine(startX, startY, endX, endY);
                        mp.repaint();
                        switch (color){
                            case "蓝色":
                                g.setColor(Color.BLUE);
                                break;
                            case "粉色":
                                g.setColor(Color.PINK);
                                break;
                            case "绿色":
                                g.setColor(Color.GREEN);
                                break;
                        }
                        g.drawLine(startX, startY, endX, endY);
                        mp.repaint();
                        break;
                    case "清除":
                        g.clearRect(0, 0, 700, 560);
                        break;
                    case "直线":
                        g.drawLine(startX, startY, endX, endY);
                        mp.repaint();
                        switch (color){
                            case "蓝色":
                                g.setColor(Color.BLUE);
                                break;
                            case "粉色":
                                g.setColor(Color.PINK);
                                break;
                            case "绿色":
                                g.setColor(Color.GREEN);
                                break;
                        }
                        g.drawLine(startX, startY, endX, endY);
                        mp.repaint();
                        break;
                    case "矩形":
                        g.drawRect(startX, startY, endX - startX, endY - startY);
                        mp.repaint();
                        switch (color){
                            case "蓝色":
                                g.setColor(Color.BLUE);
                                break;
                            case "粉色":
                                g.setColor(Color.PINK);
                                break;
                            case "绿色":
                                g.setColor(Color.GREEN);
                                break;
                        }
                        g.drawRect(startX, startY, endX - startX, endY - startY);
                        mp.repaint();
                        break;
                    case "圆形":
                        g.drawOval(startX, startY, endX - startX, endY - startY);
                        mp.repaint();
                        switch (color){
                            case "蓝色":
                                g.setColor(Color.BLUE);
                                break;
                            case "粉色":
                                g.setColor(Color.PINK);
                                break;
                            case "绿色":
                                g.setColor(Color.GREEN);
                                break;
                        }
                        g.drawOval(startX, startY, endX - startX, endY - startY);
                        mp.repaint();
                        break;
                }
            }
        });

        frame.add(panel, BorderLayout.NORTH);
        frame.add(mp, BorderLayout.CENTER);

        frame.setSize(700, 600);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Draw().start();
    }
}
